package com.controller;

import com.entity.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(@RequestParam String name, @RequestParam String pwd, Model model, HttpSession session){
        User user = userService.getUserById(name);
        if (user == null || !pwd.equals(user.getPassword())){

            model.addAttribute("ret","用户名或密码错误");
            return "login";
        }
        session.setAttribute("user",user);
        if (user.getRight().equals("0")){
            return "redirect:/index";
        }else{
            return "main";
        }

    }

    @RequestMapping("/logpage")
    public String logpage(){
        return "login";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/index";
    }

}
